package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;

public class InputController {
    public static final int MIN_MENU_OPTION = 0;
    private final BufferedReader bufferedReader;

    public InputController(BufferedReader bufferedReader) {
        this.bufferedReader = bufferedReader;
    }

    public int readOption(int maxMenuOption) throws IOException {
        final int menuOption;
        String input = "1";
        if (maxMenuOption > 1) {
            input = bufferedReader.readLine();
        }
        try {

            menuOption = Integer.parseInt(input);
            if (menuOption < MIN_MENU_OPTION || menuOption > maxMenuOption) {
                throw new IncorrectOptionSelected("Select a valid option!!");
            }

        } catch (NumberFormatException numberFormatException) {
            throw new IncorrectOptionSelected(numberFormatException);

        }
        return menuOption;
    }

    public int readLibraryNumber() throws IOException {
        String input = bufferedReader.readLine();
        final String replace = input.replace("-", "");
        try {
            return Integer.parseInt(replace);
        } catch (NumberFormatException numberFormatException) {
            throw new IncorrectOptionSelected(numberFormatException);
        }

    }

    public String readPassword() throws IOException {
        return bufferedReader.readLine();

    }
}
